﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.Sdl;

namespace Ommf
{
    public class AudioManager
    {
        private OmmfEngine engine;
        private List<Music> musics = new List<Music>();
        private List<Sound> sounds = new List<Sound>();

        internal AudioManager(OmmfEngine engine)
        {
            this.engine = engine;

            SdlMixer.Mix_OpenAudio(44100, (short)SdlMixer.MIX_DEFAULT_FORMAT, 2, 1024);
        }

        internal void Close()
        {
            DisposeMedia();
            SdlMixer.Mix_CloseAudio();
        }

        public void DisposeMedia()
        {
            foreach (Music music in musics)
                music.Dispose();

            foreach (Sound sound in sounds)
                sound.Dispose();

            musics.Clear();
            sounds.Clear();
        }

        public Music CreateMusic(string filename)
        {
            Music music = new Music(filename);
            musics.Add(music);
            return music;
        }

        public Sound CreateSound(string filename)
        {
            Sound sound = new Sound(filename);
            sounds.Add(sound);
            return sound;
        }

        public void Play(Music music)
        {
            music.Play(true);
        }

        public void Play(Music music, bool loop)
        {
            music.Play(loop);
        }

        public void Play(Sound sound)
        {
            sound.Play(false);
        }

        public void Play(Sound sound, bool loop)
        {
            sound.Play(loop);
        }

        public void Pause()
        {
            PauseMusic();
            PauseSound();
        }

        public void PauseMusic()
        {
            SdlMixer.Mix_PauseMusic();
        }

        public void PauseSound()
        {
            SdlMixer.Mix_Pause(-1);
        }

        public void Resume()
        {
            ResumeMusic();
            ResumeSound();
        }

        public void ResumeMusic()
        {
            SdlMixer.Mix_ResumeMusic();
        }

        public void ResumeSound()
        {
            SdlMixer.Mix_Resume(-1);
        }

        public void Stop()
        {
            StopMusic();
            StopSound();
        }

        public void StopMusic()
        {
            SdlMixer.Mix_HaltMusic();
        }

        public void StopSound()
        {
            SdlMixer.Mix_HaltChannel(-1);
        }

        public void DisposeMusic(Music music)
        {
            musics.Remove(music);
            music.Dispose();
        }

        public void DisposeSound(Sound sound)
        {
            sounds.Remove(sound);
            sound.Dispose();
        }
    }
}
